package com.mrlee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Account;
import com.mrlee.bean.Service;
import com.mrlee.mapper.AccountMapper;
import com.mrlee.mapper.ServiceMapper;
import com.mrlee.service.ServiceService;
import com.mrlee.utils.AjaxResult;
import com.mrlee.utils.StrUtils;
import com.mrlee.utils.TimeUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/15.
 */
@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {

    @Resource
    private ServiceMapper serviceMapper;
    @Resource
    private AccountMapper accountMapper;

    /**
     * 根据账务账号Id查找业务
     * @param aid
     * @return
     */
    @Override
    public List<Service> selectbyaid(Integer aid) {
        return serviceMapper.selectSeviceByAccountId(aid);
    }

    /**
     * 根据业务Id查找业务
     * @param sid
     * @return
     */
    @Override
    public Service selectByPrimaryKey(Integer sid) {
        Service service = serviceMapper.selectByPrimaryKey(sid);
        service.setCreateDate((serviceMapper.selectByPrimaryKey(sid).getCreateDate()).substring(0,7));
        return service;
    }

    /***
     * 查询全部业务
     * @return
     */
    @Override
    public AjaxResult selectAllService() {
        AjaxResult ajaxResult = new AjaxResult();
        List<Service> serviceList = serviceMapper.selectAllService();
        if (serviceList == null) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("查询失败");
            return ajaxResult;
        }
        ajaxResult.setCode("1");
        ajaxResult.setMessage("查询成功");
        ajaxResult.setData(serviceList);
        return ajaxResult;
    }

    /**
     * 修改状态
     * @param serviceId 业务Id
     * @param status 状态
     * @param accountId 账务账号ID
     *
     * @return
     */
    @Override
    public AjaxResult changeStatus(Integer serviceId, Integer status, Integer accountId) {
        Account account = accountMapper.selectAccountById(accountId);
        AjaxResult result = new AjaxResult();
        if (!("1".equals(account.getStatus()))){
            result.setCode("0");
            result.setMessage("账务账号已暂停或删除");
            return result;
        }
        Integer newStatus = null;
        //1代表开通状态  2代表暂停状态  0代表删除状态
        //status代表当前状态,newStatus代表改变后的状态
        if (status == 1) {
            newStatus = 2;
            serviceMapper.updatePauseDate(serviceId, TimeUtils.time());
        }
        if (status == 2) {
            newStatus = 1;
            serviceMapper.updatePauseDate(serviceId, null);
        }
        if (status == 0) {
            newStatus = 0;
            serviceMapper.updateCloseDate(serviceId,TimeUtils.time());
        }

        Integer num = serviceMapper.updateStatus(serviceId, newStatus);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("更改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("更改成功");
        return result;
    }

    /**
     * 添加业务
     * @param service 业务信息
     * @return
     */
    @Override
    public AjaxResult addService(Service service) {
        AjaxResult result = new AjaxResult();
        boolean isOsName = StrUtils.isLetterNum_Right(service.getOsUsername());
        boolean isPassword = StrUtils.isLetterNum_Right(service.getLoginPasswd());
        boolean isIp = StrUtils.isIp(service.getUnixHost());

        if (!(isOsName && isPassword && isIp && (service.getOsUsername().length() <= 8)  && (service.getLoginPasswd().length() <= 30))) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }

        service.setCreateDate(TimeUtils.time());
        service.setStatus("1");
        int count = serviceMapper.insert(service);
        if (count == 0) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }

        result.setCode("1");
        result.setMessage("添加成功");
        return result;
    }

    /**
     * 根据业务ID查找业务
     * @param serviceId
     * @return
     */
    @Override
    public AjaxResult selectService(Integer serviceId) {
        AjaxResult result = new AjaxResult();
        Service service = serviceMapper.selectByPrimaryKey(serviceId);
        if (service == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(service);
        return result;
    }

    /**
     * 搜索
     * @param osname
     * @param cardid
     * @param status
     * @param ip
     * @return
     */
    @Override
    public AjaxResult search(String osname, String cardid, String status, String ip) {
        AjaxResult result = new AjaxResult();

        List<Service> serviceList = new ArrayList<Service>();
        if (!("".equals(osname))) {
            Service service = serviceMapper.selectServiceByOsName(osname);
            serviceList.add(service);
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(serviceList);
            return result;
        }
        if (!("".equals(ip))) {
            Service service = serviceMapper.selectServiceByIp(ip);
            serviceList.add(service);
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(serviceList);
            return result;
        }
        if (!("".equals(cardid))) {
            serviceList = serviceMapper.selectServiceByCardId(cardid);
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(serviceList);
            return result;
        }
        if ("全部".equals(status)) {
            if ("".equals(cardid)) {
                serviceList = serviceMapper.selectAllService();
            } else if (!("".equals(cardid))) {
                serviceList = serviceMapper.selectServiceByCardId(cardid);
            }
        } else {
            String newstatus = "";
            if ("开通".equals(status)){
                newstatus = "1";
            }else if ("暂停".equals(status)){
                newstatus = "2";
            }else if ("删除".equals(status)){
                newstatus = "0";
            }
            List<Service> services = serviceMapper.selectServiceByStatus(newstatus);
            if ("".equals(cardid)) {
                serviceList = services;
            } else {
                for (Service service : services) {
                    System.out.println(service.getAccountId());
                    if (cardid.equals(service.getAccountId())){
                        serviceList.add(service);
                    }
                }
            }

        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(serviceList);
        return result;
    }

    /**
     * 动态搜索
     * @param osname
     * @param cardid
     * @param status
     * @param ip
     * @return
     */
    @Override
    public AjaxResult dynamicSearch(String osname, String cardid, String status, String ip) {
        if ("开通".equals(status)){
            status = "1";
        }else if ("暂停".equals(status)){
            status = "2";
        }else if ("删除".equals(status)){
            status = "0";
        }else {
            status = null;
        }

        osname = osname.isEmpty()? null :osname;
        cardid = cardid.isEmpty()? null :cardid;
        ip = ip.isEmpty()? null :ip;

        List<Service> serviceList = serviceMapper.dynamicSearch(osname,cardid,status,ip);
        for (Service service : serviceList) {
            System.out.println(service+"ser");
        }
        AjaxResult result = new AjaxResult();
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(serviceList);
        return result;
    }

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Service> queryServiceByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo,pageSize);
        List<Service> serviceList = serviceMapper.selectAllService();
        PageInfo<Service> rolePageInfo = new PageInfo<Service>(serviceList);
        return rolePageInfo;
    }


}
